package com.tarena.vehicle.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.vehicle.common.ex.ServiceException;
import com.tarena.vehicle.common.pojo.vo.PageData;
import com.tarena.vehicle.common.util.PageInfoToPageDataConverter;
import com.tarena.vehicle.common.web.ServiceCode;
import com.tarena.vehicle.management.converter.VehicleInspectionConverter;
import com.tarena.vehicle.management.mapper.VehicleInspectionMapper;
import com.tarena.vehicle.management.pojo.entity.VehicleInspection;
import com.tarena.vehicle.management.pojo.param.VehicleInspectionAddNewParam;
import com.tarena.vehicle.management.pojo.param.VehicleInspectionUpdateParam;
import com.tarena.vehicle.management.pojo.query.VehicleInspectionQuery;
import com.tarena.vehicle.management.pojo.vo.VehicleInspectionListItemVO;
import com.tarena.vehicle.management.pojo.vo.VehicleInspectionStandardVO;
import com.tarena.vehicle.management.service.IVehicleInspectionService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VehicleInspectionServiceImpl implements IVehicleInspectionService {
    @Autowired
    private VehicleInspectionMapper vehicleInspectionMapper;

    @Autowired
    private VehicleInspectionConverter vehicleInspectionConverter;
    private static final Logger logger = LoggerFactory.getLogger(VehicleInspectionServiceImpl.class);

    @Override public void addNew(VehicleInspectionAddNewParam param) {
        logger.info("接收到的年检证书信息如下{}", param);

        String inspectionNum = param.getInspectionNum();
        int count = vehicleInspectionMapper.getCountByInspectionNum(inspectionNum);
        if (count > 0) {
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, "当前年检证书已经录入");
        }
        VehicleInspection vehicleInspection = vehicleInspectionConverter.param2po(param);

        int row = vehicleInspectionMapper.insert(vehicleInspection);

        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "添加车辆年检信息失败，服务器忙，请稍后再尝试！");
        }

    }

    @Override public VehicleInspectionStandardVO getVehicleInspectionById(Integer id) {
        VehicleInspectionStandardVO vo = vehicleInspectionMapper.getVehicleInspectionById(id);
        if (vo == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆年检信息不存在");
        }
        return vo;
    }

    @Override public PageData<VehicleInspectionListItemVO> getVehicleInspectionList(VehicleInspectionQuery query,Integer num, Integer size) {
        if (num != null && size != null)
            PageHelper.startPage(num, size);
        VehicleInspection vehicleInspection = vehicleInspectionConverter.query2po(query);
        List<VehicleInspectionListItemVO> list =vehicleInspectionMapper.getVehicleInspectionList(vehicleInspection);
        PageInfo<VehicleInspectionListItemVO> info = new PageInfo<>(list);
        PageData<VehicleInspectionListItemVO> pageData = PageInfoToPageDataConverter.convert(info);
        return pageData;
    }

    @Override public void setPass(Integer id) {
        VehicleInspectionStandardVO vo = vehicleInspectionMapper.getVehicleInspectionById(id);
        if (vo == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆年检信息不存在");
        }
        if (vo.getStatus()==0){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, "该车辆年检状态已为通过");
        }
        int row=vehicleInspectionMapper.updateStatusPass(id);
        if (row != 1){
            throw new ServiceException(ServiceCode.ERROR_INSERT, "修改车辆年检状态失败，服务器忙，请稍后再尝试！");
        }
    }

    @Override public void setNotPass(Integer id) {
        VehicleInspectionStandardVO vo = vehicleInspectionMapper.getVehicleInspectionById(id);
        if (vo == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆年检信息不存在");
        }
        if (vo.getStatus()==1){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, "该车辆年检状态已为未通过");
        }
        int row=vehicleInspectionMapper.updateStatusNotPass(id);
        if (row != 1){
            throw new ServiceException(ServiceCode.ERROR_INSERT, "修改车辆年检状态失败，服务器忙，请稍后再尝试！");
        }
    }

    @Override public void updateVehicleInspection(VehicleInspectionUpdateParam param) {
        VehicleInspectionStandardVO vo = vehicleInspectionMapper.getVehicleInspectionById(param.getId());
        if (vo == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆年检信息不存在");
        }
        VehicleInspection vehicleInspection = vehicleInspectionConverter.param2po(param);
        int row=vehicleInspectionMapper.updateVehicleInspection(vehicleInspection);
        if (row < 1){
            throw new ServiceException(ServiceCode.ERROR_INSERT, "修改车辆年检信息失败，服务器忙，请稍后再尝试！");
        }

    }

    @Override public void deleteVehicleInspectionById(Integer id) {
        VehicleInspectionStandardVO vo = vehicleInspectionMapper.getVehicleInspectionById(id);
        if (vo == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆年检信息不存在");
        }
        int row=vehicleInspectionMapper.deleteVehicleInspectionById(id);
        if (row < 1){
            throw new ServiceException(ServiceCode.ERROR_INSERT, "删除该车辆年检信息失败，服务器忙，请稍后再尝试！");
        }
    }
}
